// src\views\sendbox\new-manage\NewsDraft.jsx
// 草稿箱
import React, { useEffect, useState } from 'react'
import { Button, Space, Table,message,Modal,notification} from 'antd';
import axios from 'axios';
import { DeleteOutlined,EditOutlined,ArrowUpOutlined,ExclamationCircleOutlined} from '@ant-design/icons';
export default function NewsDraft(props) {
  const [dataSource,setDataSource]=useState([]);
  const { confirm } = Modal;
  const columns = [
    {
      title: 'ID',
      dataIndex: 'id',
      key: 'id',
    },
    {
      title: '新闻标题',
      dataIndex: 'title',
      key: 'title',
      render:(title,item)=>{
        return  <Button type="link" onClick={()=>{props.history.push(`/news-manage/preview/${item.id}`)}}>{title}</Button>
      }
    },
    {
      title: '作者',
      dataIndex: 'author',
      key: 'author',
    },
    {
      title: '分类',
      dataIndex: 'category',
      key: 'category',
      render:(_,item)=>{
        return item.category.title
      }
    },
    {
      title: '操作',
      dataIndex: 'operate',
      key: 'operate',
      render:(_,item,index)=>{
        return (
          <Space>
            <Button danger shape="circle" icon={<DeleteOutlined />} size="small" onClick={()=>handleDel(item)}/>
            <Button shape="circle" icon={<EditOutlined />} size="small" onClick={()=>{props.history.push(`/news-manage/update/${item.id}`)}}/>
            <Button type="primary" shape="circle" icon={<ArrowUpOutlined />} size="small" onClick={()=>handleCheck(item)}/>
          </Space>
        )
      }
    },
  ];
  useEffect(()=>{
  const User=JSON.parse(localStorage.getItem('token'));
    axios.get(`http://localhost:8888/news?author=${User.username}&auditState=0&_expand=category`).then(res=>{
      setDataSource(res.data);
    })
  },[]);
  // 删除
  const handleDel=(item)=>{
    confirm({
      title: '您确定删除这条草稿吗?',
      icon: <ExclamationCircleOutlined />,
      content: '',
      okText: '确定',
      okType: 'danger',
      cancelText: '取消',
      onOk() {
        detleteMethod(item);
      },
      onCancel() {
        console.log('Cancel');
      },
    });
    
  }
  const detleteMethod=(item)=>{
    axios.delete(`http://localhost:8888/news/${item.id}`).then(res=>{
      message.success('草稿删除成功！');
    })
  }
  // 审核
  const handleCheck=(item)=>{
    axios.patch(`http://localhost:8888/news/${item.id}`,{
      auditState:1
  }).then(res=>{
      props.history.push('/audit-manage/list')

      notification.info({
          message: `通知`,
          description:
            `您可以到${'审核列表'}中查看您的新闻`,
          placement:"bottomRight"
      });
  })
  }
  return (
    <div>
      <Table dataSource={dataSource} columns={columns} rowKey={item=>item.id}/>;
    </div>
  )
}

